<script setup>
import { TOKEN } from '@/assets/js/config'
import { storage } from '@/utils/utils'
import { onLaunch, onPageNotFound, onHide, onShow } from '@dcloudio/uni-app'
import { getCurrentInstance } from 'vue'
import { useStore } from '@/store'

const store = useStore()
const { $https } = getCurrentInstance().appContext.config.globalProperties
// let timer = null
// #ifdef MP-WEIXIN
const updateVersions = () => {
  const updateManager = uni.getUpdateManager()
  updateManager.onCheckForUpdate((res) => {
    console.log('是否有新的版本', res.hasUpdate)
  })
  updateManager.onUpdateReady(() => {
    uni.showModal({
      title: '更新提示',
      content: '新版本已经准备好，是否更新？',
      success (res) {
        if (res.confirm) {
          updateManager.applyUpdate()
        }
      }
    })
  })
}
// #endif
onLaunch(() => {
  const token = storage.get(TOKEN)
  if (token) {
    $https({
      url: '/profile',
      type: '',
      data: {
        jump: 'false',
        type: 1
      }
    }).then(res => {
      if (res.code === 0) {
        store.commit('setProfile', res.data)
      }
    })
  }
  // #ifdef MP-WEIXIN
  updateVersions()
  // #endif
})
onPageNotFound(() => {
  uni.redirectTo({
    url: '/pages/error'
  })
})
onShow(() => {
  if (uni.getStorageSync(TOKEN)) {
    /* uni.authorize({
      scope: 'scope.userLocation',
      success: () => {
        uni.getLocation({
          type: 'gcj02', // 定位的坐标类型，'wgs84'表示 GPS 坐标，'gcj02'表示国测局坐标
          altitude: false, // 是否获取高度信息
          success: (res) => {
            // 处理地理位置数据
            $https({
              url: '/app/geo',
              type: '',
              method: 'post',
              data: { lat: res.latitude, lng: res.longitude }
            })
          },
          fail: (err) => {
            console.log('获取地理位置失败:', err)
          }
        })
      }
    }) */
  }
})
onHide(() => {
  uni.setStorageSync('isClearProcess', true)
})
</script>

<style lang="scss">
/*每个页面公共css */
@import "@/assets/styles/common.scss";
.natv-button {
  /* display: none; */
  margin: 0;
  padding: 0;
  background-color: #fff;
  border: none;
  line-height: 28upx;
  font-size: 28upx;
  font-style: normal;
  border: none;
  &:after{
    border: none;
  }
}
button{
  &::after {
    border: none;
  }
}
</style>
